﻿cmake_minimum_required(VERSION 3.5)

# 项目名称
project(QtApp)

set(APP_TARGET "QtApp")
# 设置ROOT目录
set(PROJECT_ROOT "${CMAKE_CURRENT_SOURCE_DIR}")

set(CMAKE_CXX_STANDARD 11)
# 设置跟目录include，比如某个文件#include <common/xxx.h>
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# 加载cmake子模块
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_ROOT}/cmake")

# Only generate Debug and Release configuration types.
set(CMAKE_CONFIGURATION_TYPES Debug Release)

macro(USE_QT)
    set(CMAKE_AUTOMOC ON)
    set(CMAKE_AUTOUIC ON)
    set(CMAKE_AUTORCC ON)
endmacro()

USE_QT()

# 定义一个宏，内部动态库都使用该宏进行复制到可执行目录下
macro(INSTALL_SHARED_LIB lib_name)
    install(
        TARGETS ${lib_name}
        RUNTIME DESTINATION "${ACTIVE_EXECUTE_DESTINATION}"
        )

    # 拷贝pdb文件，对于debug这个是用的到的,ok
    install(
        FILES $<TARGET_PDB_FILE:${lib_name}>
        DESTINATION "${ACTIVE_EXECUTE_DESTINATION}"
        CONFIGURATIONS Debug)
endmacro(INSTALL_SHARED_LIB)

macro(INSTALL_EXECUTE exe_name)
    install(
        TARGETS ${exe_name}
        RUNTIME DESTINATION "${ACTIVE_EXECUTE_DESTINATION}"
        )
endmacro(INSTALL_EXECUTE)

set(QTDIR "/Users/sl/Qt5.12.5/5.12.5/clang_64")

if(QTDIR)
    set(CMAKE_PREFIX_PATH ${QTDIR})
else()
    message("Tips: If you not using qtcreator,you may need to set QTDIR.")
    message("Tips: In MAC like YOURPATH/Qt5.9.7/5.9.7/clang_64")
    message("Tips: In Win like YOURPATH/Qt/Qt5.9.7/5.9.7/msvc2015")
endif()

# Use folders in the resulting project files.
set_property(GLOBAL PROPERTY OS_FOLDERS ON)

set(CMAKE_SKIP_BUILD_RPATH FALSE)

find_package(Qt5 COMPONENTS Core Gui Widgets Quick REQUIRED)

find_package(Crashpad REQUIRED)
find_package(Mqtt REQUIRED)

# 设置目标execute目录
if (WIN32)
    if (${CMAKE_GENERATOR} STREQUAL "NMake Makefiles")
        set(ACTIVE_EXECUTE_DESTINATION ${PROJECT_BINARY_DIR})
    elseif(${CMAKE_GENERATOR} STREQUAL "Ninja")
        set(ACTIVE_EXECUTE_DESTINATION ${PROJECT_BINARY_DIR})
    else()
        # vs默认的PROJECT_BINARY_DIR默认为build/Release或Debug下
        set(ACTIVE_EXECUTE_DESTINATION ${PROJECT_BINARY_DIR}/$<CONFIGURATION>)
    endif()
else()

endif()

set(CMAKE_MSVCIDE_RUN_PATH "${QTDIR}/bin")

message("Generate Type: ${CMAKE_GENERATOR}")
message("Binary Dir: ${PROJECT_BINARY_DIR}")
message("Qt Path: ${QTDIR}")

include_directories(${PROJECT_ROOT}/log/spdlog/include)
include_directories(${MQTT_INCLUDE_DIR})

set(APP_COMMON_SRCS
    ${PROJECT_ROOT}/common/HashList.h
    ${PROJECT_ROOT}/common/HashList.cpp
    ${PROJECT_ROOT}/common/QStringExt.h
    ${PROJECT_ROOT}/common/QtAppMacro.h
    ${PROJECT_ROOT}/common/SingletonClass.h
    )

set(APP_HELPER_SRCS
    ${PROJECT_ROOT}/helper/ConfigHelper.h
    ${PROJECT_ROOT}/helper/ConfigHelper.cpp
    ${PROJECT_ROOT}/helper/DirHelper.h
    ${PROJECT_ROOT}/helper/DirHelper.cpp
    )

set(APP_LOG_SRCS
    ${PROJECT_ROOT}/log/TSpdlog.h
    ${PROJECT_ROOT}/log/TSpdlog.cpp
    ${PROJECT_ROOT}/log/TSpdlogAssistant.h
    ${PROJECT_ROOT}/log/TSpdlogAssistant.cpp
    )

set(APP_NET_SRCS
    ${PROJECT_ROOT}/net/MqttCommon.h
    ${PROJECT_ROOT}/net/MqttCommon.cpp
    ${PROJECT_ROOT}/net/MqttWrapper.h
    ${PROJECT_ROOT}/net/MqttWrapper.cpp
    )

set(APP_UI_WIDGET_SRCS
    ${PROJECT_ROOT}/ui_widget/widget.h
    ${PROJECT_ROOT}/ui_widget/widget.cpp
    )

set(APP_MAIN_SRCS
    ${PROJECT_ROOT}/Application.h
    ${PROJECT_ROOT}/Application.cpp
    ${PROJECT_ROOT}/main.cpp
    )

# 添加QRC文件
qt5_add_resources(
    QRC_FILES
    ${PROJECT_ROOT}/qml.qrc
    )

if (MSVC)
    add_executable(${APP_TARGET} WIN32
        ${APP_COMMON_SRCS}
        ${APP_HELPER_SRCS}
        ${APP_LOG_SRCS}
        ${APP_NET_SRCS}
        ${APP_UI_WIDGET_SRCS}
        ${APP_MAIN_SRCS}
        ${QRC_FILES}
        )
else()
    message("mac")
    find_library(FOUNDATION Foundation)
    find_library(COREFOUNDATION CoreFoundation)
    find_library(COREGRAPHICS CoreGraphics)
    find_library(CORETEXT CoreText)
    find_library(SECURITY Security)
    find_library(IOKIT IOKit)
    find_library(BSM bsm)

    add_executable(${APP_TARGET}
        ${APP_COMMON_SRCS}
        ${APP_HELPER_SRCS}
        ${APP_LOG_SRCS}
        ${APP_NET_SRCS}
        ${APP_UI_WIDGET_SRCS}
        ${APP_MAIN_SRCS}
        ${QRC_FILES}
        )

    target_link_libraries(
        ${APP_TARGET}
        ${FOUNDATION}
        ${COREFOUNDATION}
        ${COREGRAPHICS}
        ${CORETEXT}
        ${SECURITY}
        ${IOKIT}
        ${BSM}
        )
endif()

#set_property(TARGET ${APP_TARGET} PROPERTY OUTPUT_NAME ${APP_TARGET})

# 设置输出名称
#set_property(TARGET ${PROJECT_NAME} PROPERTY OUTPUT_NAME ${APP_OUTPUT_NAME})
#set_target_properties(${APP_TARGET} PROPERTIES INSTALL_RPATH_USE_LINK_PATH TRUE)
#message("项目名称："${PROJECT_NAME})

if(MSVC)
    # crashpad
    target_link_libraries(
        ${APP_TARGET}
        debug ${CRASHPAD_BASE_LIBRARY_DEBUG}
        debug ${CRASHPAD_CLIENT_LIBRARY_DEBUG}
        debug ${CRASHPAD_UTIL_LIBRARY_DEBUG}
        optimized ${CRASHPAD_BASE_LIBRARY_RELEASE}
        optimized ${CRASHPAD_CLIENT_LIBRARY_RELEASE}
        optimized ${CRASHPAD_UTIL_LIBRARY_RELEASE}
        )

else(MSVC)
    # crashpad
    target_link_libraries(
        ${APP_TARGET}
        ${CRASHPAD_BASE_LIBRARY_RELEASE}
        ${CRASHPAD_CLIENT_LIBRARY_RELEASE}
        ${CRASHPAD_UTIL_LIBRARY_RELEASE}
        )
endif(MSVC)

# mqtt
target_link_libraries(
    ${APP_TARGET}
    ${MQTT_MOSQUITTO_LIBRARY_RELEASE_DEBUG}
    ${MQTT_MOSQUITTOPP_LIBRARY_RELEASE_DEBUG}
    )

target_link_libraries(
    ${APP_TARGET}
    Qt5::Core
    Qt5::Widgets
    Qt5::Gui
    Qt5::Quick
    )

################################ 拷贝动态库及资源文件到Active可执行目录下 ################################

# copy crashpad 拷贝时要带斜杠，不然会带上本身如bin或release，debug等
if (WIN32)
    install(DIRECTORY "${PROJECT_ROOT}/libs/crashpad/win/bin/"
        DESTINATION ${ACTIVE_EXECUTE_DESTINATION} CONFIGURATIONS Release)

    install(DIRECTORY "${PROJECT_ROOT}/libs/crashpad/win/bin/"
        DESTINATION ${ACTIVE_EXECUTE_DESTINATION} CONFIGURATIONS Debug)

    install(DIRECTORY "${PROJECT_ROOT}/libs/crashpad/win/lib/release/"
        DESTINATION ${ACTIVE_EXECUTE_DESTINATION} CONFIGURATIONS Release)

    install(DIRECTORY "${PROJECT_ROOT}/libs/crashpad/win/lib/debug/"
        DESTINATION ${ACTIVE_EXECUTE_DESTINATION} CONFIGURATIONS Debug)

    # copy mqtt
    install(DIRECTORY "${PROJECT_ROOT}/libs/mqtt/bin/"
        DESTINATION ${ACTIVE_EXECUTE_DESTINATION} CONFIGURATIONS Debug)

    install(DIRECTORY "${PROJECT_ROOT}/libs/mqtt/bin/"
        DESTINATION ${ACTIVE_EXECUTE_DESTINATION} CONFIGURATIONS Release)
else()

endif()
